package main.java.com.ThinkInJava.Chapter21并发._21_5_3生产者与消费者;

import java.util.concurrent.TimeUnit;

/**
 * 测试
 */
public class Chef implements Runnable{
    private Restaurant restaurant;
    private int count = 0;
    public Chef(Restaurant r) {
        restaurant = r;
    }
    @Override
    public void run() {
        try {
            while (!Thread.interrupted()) {
                synchronized (this) {
                    while (restaurant.meal != null) {
                        wait();
                    }
                }
                if (++count == 10) {
                    System.out.println("Out of food,closing");
                    restaurant.exec.shutdownNow();
                }
                System.out.println("Order up!");
                synchronized (restaurant.waitPersion) {
                    restaurant.meal = new Meal(count);
                    restaurant.waitPersion.notifyAll();
                }
                TimeUnit.MILLISECONDS.sleep(100);
            }
        } catch (InterruptedException e) {
            System.out.println("Chef interrupted");
            e.printStackTrace();
        }
    }
}
